package com.hudilao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hudilao.dto.OrderReviewDTO;
import com.hudilao.dto.OrderReviewDetailDTO;
import com.hudilao.entity.OrderReview;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderReviewMapper extends BaseMapper<OrderReview> {

    /**
     * 用户端，根据订单id获取对应的订单评论
     * @param orderId 订单号,orders中的id
     * @return 订单评论
     */
    @Select("SELECT orv.review_text " +
            "FROM order_review orv " +
            "JOIN orders ord ON orv.order_id = ord.id " +
            "WHERE orv.order_id = #{orderId}")
    String getReviewTextByOrderId(long orderId);

    @Select("SELECT orv.employee_reply " +
            "FROM order_review orv " +
            "JOIN orders ord ON orv.order_id = ord.id " +
            "WHERE orv.order_id = #{orderId}")
    String getReviewReplyByOrderId(long orderId);


    //管理端：评价管理
    /**
     * 分页查询所有订单评价，包括顾客名称和评价内容
     * @param page 分页参数
     * @return 分页结果
     */
    @Select("SELECT r.id, r.order_id, r.create_time, r.rating,r.status, r.review_text,r.employee_reply ,cm.name AS customer_name " +
            "FROM order_review r " +
            "LEFT JOIN customer_membership cm ON r.customer_id = cm.customer_id " +
            "ORDER BY r.create_time DESC")
    Page<OrderReviewDTO> selectOrderReviews(Page<?> page);

    /**
     * 根据订单号查询订单评价，包括顾客名称和评价内容
     * @param orderId 订单号
     * @return 订单评价列表
     */
    @Select("SELECT r.id, r.order_id, r.create_time, r.rating, r.review_text, r.employee_reply,cm.name AS customer_name " +
            "FROM order_review r " +
            "LEFT JOIN customer_membership cm ON r.customer_id = cm.customer_id " +
            "WHERE r.order_id = #{orderId}")
    List<OrderReviewDTO> selectOrderReviewByOrderId(@Param("orderId") Long orderId);


    /**
     * 根据评价id筛选评价详情
     * @param orderId 订单号
     * @return 订单评价列表
     */
    @Select("SELECT r.id AS reviewId, r.order_id AS orderId, r.rating, r.review_text AS reviewText, " +
            "r.employee_reply AS employeeReply, r.create_time AS createTime, " +
            "c.name AS customerName " +
            "FROM order_review r " +
            "LEFT JOIN customer_membership c ON r.customer_id = c.customer_id " +
            "WHERE r.order_id = #{orderId}")
    OrderReviewDetailDTO selectOrderReviewDetailByReviewId(@Param("orderId") Long orderId);


    // 通过 order_id 更新评价
    @Update("UPDATE order_review SET employee_reply = #{employeeReply} , status = #{status} WHERE order_id = #{orderId}")
    void updateByOrderId(@Param("orderId") Long orderId, @Param("employeeReply") String employeeReply,@Param("status") int status);

}
